/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.detail;

import com.siro.pm.I18nManager;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Project;
import com.siro.pm.model.provider.ProjectProvider;
import com.siro.pm.ui.main.ProjectPage;
import com.siro.pm.utils.DeveloperBrowser;
import com.siro.pm.utils.UUIDGenerator;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since ProjectDetailPage.java v1.0
 */
public class ProjectDetailPage extends VerticalLayout {
	private static final long serialVersionUID = 5805653924799037155L;

	private Window window;
	private ProjectPage projectPage;
	private Project project;

	TextField name;
	TextField manager;
	TextField partnerLeader;
	TextField startTime;
	TextField estimateEndTime;
	ComboBox status;
	ComboBox procces;

	ComboBox managerDeveloper;
	ComboBox partnerDeveloper;

	Button button_Save;

	public ProjectDetailPage() {
	}

	public ProjectDetailPage(Project project) {
		this.project = project;
	}

	public void createContents() {
		setMargin(true);
		setSpacing(true);

		// base info
		GridLayout projectBaseInfo = new GridLayout();
		projectBaseInfo.setStyleName("h2");
		projectBaseInfo.setCaption(I18nManager.getMessage("project.base.info"));
		projectBaseInfo.setColumns(2);
		projectBaseInfo.setMargin(true);
		projectBaseInfo.setSpacing(true);
		projectBaseInfo.setWidth("100%");

		name = new TextField(ProjectProvider.PROJECT_PROPERTY_PROJECTNAME);
		name.setWidth("100%");
		projectBaseInfo.addComponent(name);

		startTime = new TextField(ProjectProvider.PROJECT_PROPERTY_STARTTIME);
		startTime.setWidth("100%");
		projectBaseInfo.addComponent(startTime);

		HorizontalLayout managerLayout = new HorizontalLayout();
		managerLayout.setWidth("100%");
		manager = new TextField(ProjectProvider.PROJECT_PROPERTY_PROJECTMANAGER);
		manager.setWidth("98%");
		managerLayout.addComponent(manager);
		managerLayout.setExpandRatio(manager, 1);
		managerDeveloper = new ComboBox(" ");
		DeveloperBrowser.configDeveloperBrowser(managerDeveloper, project);
		managerLayout.addComponent(managerDeveloper);
		projectBaseInfo.addComponent(managerLayout);

		HorizontalLayout partnerLayout = new HorizontalLayout();
		partnerLayout.setWidth("100%");
		partnerLeader = new TextField(
				ProjectProvider.PROJECT_PROPERTY_PARTNERLEADER);
		partnerLeader.setWidth("98%");
		partnerLayout.addComponent(partnerLeader);
		partnerLayout.setExpandRatio(partnerLeader, 1);
		partnerDeveloper = new ComboBox(" ");
		DeveloperBrowser.configDeveloperBrowser(partnerDeveloper, project);
		partnerLayout.addComponent(partnerDeveloper);
		projectBaseInfo.addComponent(partnerLayout);

		estimateEndTime = new TextField(
				ProjectProvider.PROJECT_PROPERTY_ESTIMATEENDTIME);
		estimateEndTime.setWidth("100%");
		projectBaseInfo.addComponent(estimateEndTime);

		// process info
		GridLayout projectProcessInfo = new GridLayout();
		projectProcessInfo.setStyleName("h2");
		projectProcessInfo.setCaption(I18nManager
				.getMessage("project.process.info"));
		projectProcessInfo.setColumns(2);
		projectProcessInfo.setMargin(true);
		projectProcessInfo.setSpacing(true);
		projectProcessInfo.setWidth("100%");

		status = new ComboBox(ProjectProvider.PROJECT_PROPERTY_STATUS);
		String[] statusData = ProjectProvider.PROJECT_STATUS_DATA;
		for (int i = 0; i < statusData.length; i++) {
			status.addItem(statusData[i]);
		}
		status.setWidth("100%");
		status.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
		status.setImmediate(true);
		projectProcessInfo.addComponent(status);

		procces = new ComboBox(ProjectProvider.PROJECT_PROPERTY_PROCESS);
		String[] processData = ProjectProvider.PROJECT_PROCESS_DATA;
		for (int i = 0; i < processData.length; i++) {
			procces.addItem(processData[i]);
		}
		procces.setWidth("100%");
		procces.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
		procces.setImmediate(true);
		projectProcessInfo.addComponent(procces);

		// buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setMargin(true);
		buttons.setSpacing(true);
		buttons.setWidth("100%");

		button_Save = new Button(I18nManager.getMessage("button.save"));
		button_Save.setWidth(50, Sizeable.UNITS_PIXELS);
		button_Save.addListener(new ButtonClickListener());
		buttons.addComponent(button_Save);
		buttons.setComponentAlignment(button_Save, Alignment.MIDDLE_RIGHT);

		addComponent(projectBaseInfo);
		addComponent(projectProcessInfo);
		addComponent(buttons);

		initData();
	}

	private void initData() {
		if (project != null) {
			name.setValue(project.getName());
			manager.setValue(project.getManager().getName());
			partnerLeader.setValue(project.getPartnerLeader().getName());
			startTime.setValue(project.getStartTime());
			estimateEndTime.setValue(project.getEstimatedEndTime());
			status.select(project.getStatus());
			procces.select(project.getProcess());

			name.setEnabled(false);
			startTime.setEnabled(false);
			if (!Developer.isProjectManager(project.getManager())) {
				manager.setEnabled(false);
				partnerLeader.setEnabled(false);
				estimateEndTime.setEnabled(false);
				status.setEnabled(false);
				procces.setEnabled(false);
				managerDeveloper.setEnabled(false);
				partnerDeveloper.setEnabled(false);
				button_Save.setEnabled(false);
			}
		}
	}

	class ButtonClickListener implements ClickListener {
		private static final long serialVersionUID = -2851840644753744125L;

		public void buttonClick(ClickEvent event) {
			boolean update = true;
			if (project == null) {
				project = new Project();
				project.setId(UUIDGenerator.newUUID());
				update = false;
			}
			project.setName((String) name.getValue());
			Developer developer = (Developer) managerDeveloper.getValue();
			if (developer != null)
				project.setManager(developer);
			developer = (Developer) partnerDeveloper.getValue();
			if (developer != null)
				project.setPartnerLeader(developer);
			project.setStartTime((String) startTime.getValue());
			project.setEstimatedEndTime((String) estimateEndTime.getValue());
			project.setStatus((String) status.getValue());
			project.setProcess((String) procces.getValue());
			if (update) {
				Project.updateProject(project);
			} else {
				Project.insertProject(project);
			}
			if (projectPage != null) {
				projectPage.projectsTable
						.setContainerDataSource(ProjectProvider
								.getProjectData(null));
			}
			if (window != null) {
				(window.getParent()).removeWindow(window);
			}
		}
	}

	public Window getProjectWindow() {
		return window;
	}

	public void setProjectWindow(Window window) {
		this.window = window;
	}

	public ProjectPage getProjectPage() {
		return projectPage;
	}

	public void setProjectPage(ProjectPage projectPage) {
		this.projectPage = projectPage;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

}
